<template>
  <pro-layout
    :title="title"
    :menus="menus"
    :collapsed="collapsed"
    :mediaQuery="query"
    :isMobile="isMobile"
    :handleMediaQuery="handleMediaQuery"
    :handleCollapse="handleCollapse"
    :logo="logoRender"
    :i18nRender="i18nRender"
    :theme="$store.state.app.theme"
    :layout="$store.state.app.layout"
    :contentWidth="$store.state.app.contentWidth === 'Fixed'"
    :fixedHeader="$store.state.app.fixedHeader"
    :fixSiderbar="$store.state.app.fixedSidebar"
    :autoHideHeader="$store.state.app.autoHideHeader"
  >
    <template v-slot:rightContentRender>
      <right-content :top-menu="$store.state.app.layout === 'topmenu'" :is-mobile="isMobile" :theme="$store.state.app.theme" />
    </template>
    <template v-slot:footerRender>
      <global-footer />
    </template>
    <page-view></page-view>
  </pro-layout>
</template>

<script>
import { mapState } from 'vuex'
import { SIDEBAR_TYPE, TOGGLE_MOBILE_TYPE } from '@/store/mutation-types'

import defaultSettings from '@/config/defaultSettings'
import RightContent from '@/components/GlobalHeader/RightContent'
import GlobalFooter from '@/components/GlobalFooter'
import LogoSvg from '../assets/logo.svg?inline'
import PageView from '@/layouts/PageView'

export default {
  name: 'BasicLayout',
  components: {
    RightContent,
    GlobalFooter,
    PageView
  },
  data () {
    return {
      visible: false,
      isProPreviewSite: process.env.VUE_APP_PREVIEW === 'true' && process.env.NODE_ENV !== 'development',
      menus: [],
      collapsed: false,
      title: defaultSettings.title,
      query: {},
      isMobile: false
    }
  },
  computed: {
    ...mapState({
      mainMenu: state => state.permission.addRouters
    })
  },
  created () {
    const routes = this.mainMenu.find(item => item.path === '/')
    this.menus = (routes && routes.children) || []
    this.$watch('collapsed', () => {
      this.$store.commit(SIDEBAR_TYPE, this.collapsed)
    })
    this.$watch('isMobile', () => {
      this.$store.commit(TOGGLE_MOBILE_TYPE, this.isMobile)
    })
  },
  mounted () {
    const userAgent = navigator.userAgent
    if (userAgent.indexOf('Edge') > -1) {
      this.$nextTick(() => {
        this.collapsed = !this.collapsed
        setTimeout(() => {
          this.collapsed = !this.collapsed
        }, 16)
      })
    }
  },
  methods: {
    i18nRender (key) {
      return key
    },
    handleMediaQuery (val) {
      this.query = val
      if (this.isMobile && !val['screen-xs']) {
        this.isMobile = false
        return
      }
      if (!this.isMobile && val['screen-xs']) {
        this.isMobile = true
        this.collapsed = false
      }
    },
    handleCollapse (val) {
      this.collapsed = val
    },
    logoRender () {
      return <LogoSvg />
    }
  }
}
</script>

<style lang="less">
@import "./BasicLayout.less";
</style>
